#include<iostream>
#include<stdio.h>
#include<ctype.h>

using namespace std;

struct ca 
{
	char c;
	ca *next;
};

void InitList(ca *L) 
{
	cout<<"输入一页文字(以#结束): "<<endl;
	while((L->c = getchar()) != '#') 
	{
		L->next = new ca;
		L = L->next;
	}
	L->next = NULL;
}

void StrNo(ca *L) 
{
	int n = 0;
	char s[80], *q;
	ca *p;
	cout<<"输入一字符串: ";
	cin>>s;
	while(L->next)
	{
		for (p = L, q = s; *q != '\0' && p->next; p = p->next, q++)
			if (p->c != *q) break;
		if (*q == '\0') n++;
		L = L->next;
	}
	cout<<"字符串 "<<s<<" 出现 "<<n<<" 次\n";
}

void tihuan(ca *L)
{
	int n = 0;
	char s[80], *q;
	ca *p,*temp;
	cout<<"输入要被替换的字符串: ";
	cin>>s;
	cout<<"输入要替换成的字符串: "<<endl;
	char s0[80],*q0;
	cin>>s0;
	q0=s0;
	while(L->next)
	{
		temp=L;
		n=0;
		for (p = L, q = s; *q != '\0' && p->next; p = p->next, q++)
			if (p->c != *q) 
				break;
			else
				n++;
		if (*q == '\0') 
		{

			while(*q0!='\0')
			{
				ca *temp0;     
				if(n)
				{
					temp->c=*q0;     
					q0++;
					temp0=temp;
					temp=temp->next;
					n--;
				}
				else
				{
					ca *tt=new ca;
					tt->c=*q0;
					tt->next=temp0->next;
					temp0->next=tt;
					temp0=temp0->next;
					q0++;
				}
			}

		}
		q0=s0;
		L = L->next;
	}

}

ca *DelStr(ca *L)
{
	ca *H = L;
	char s[80], *q;
	ca *p;
	cout<<"输入该子串: ";
	cin>>s;
	if (H->c == *s)
	{
		for (p = H->next, q = s+1; *q != '\0' && p->next; p = p->next, q++)
			if (p->c != *q) break;
		if (*q == '\0') H = p;
	}
	while(L->next) 
	{
		for (p = L->next, q = s; *q != '\0' && p->next; p = p->next, q++)
			if (p->c != *q) break;
		if (*q == '\0') L->next = p;
		else L = L->next;
	}
	return H;
}

void print1(ca *L)
{
	while(L->next)
	{
		cout<<L->c;
		L = L->next;
	}
	cout<<endl;
}

void print2(ca *L, int &p1, int &p2, int &p3, int &n) 
{
	n = p1 = p2 = p3 = 0;
	for( ; L->next; L = L->next, n++)
		if(isalpha(L->c)) p1++;
		else if(isdigit(L->c)) p2++;
		else if(L->c == ' ') p3++;
		else if(L->c == '\n') n--;
	cout<<"\n全部字母数: "<<p1;
	cout<<"\n数字个数: "<<p2;
	cout<<"\n空格个数: "<<p3;
	cout<<"\n文章总字数: "<<n<<endl;
}

int main() 
{
	ca *L = new ca;
	int p1, p2, p3, n, c;
	InitList(L);
	do  
	{
		cout<<"\n0:退出\n"
			"1:统计中英文字母和空格数以及整篇文章总字数\n"
			"2:统计某一字符串在文章中出现次数\n"
			"3:删除某一子串\n"
			"4:分行输出用户输入的各行字符\n"
			"5:替换字符串\n";
		cin>>c;
		switch(c) 
		{
			case 1:print2(L, p1, p2, p3, n);break;
			case 2:StrNo(L);break;
			case 3:DelStr(L);break;
			case 4:print1(L);break;
			case 5:tihuan(L);break;
		}
	}while(c);
} 



